<template>
    <div class="app-container">
        <div class="search-container">
            <el-form ref="queryFormRef" :model="queryParams" :inline="true">

                <el-form-item style="margin-right: 15px;">
                    <el-input v-model="queryParams.name" placeholder="轮播图标题名字"></el-input>
                </el-form-item>




                <el-form-item>
                    <el-button type="primary" @click="handleQuery"><i-ep-search />搜索</el-button>
                </el-form-item>

                <el-form-item>
                    <el-button @click="add()">新增</el-button>
                </el-form-item>


            </el-form>
        </div>

        <el-card shadow="never" class="table-container">
            <el-table :data="BannerPageData" style="width: 100%" border>
                <el-table-column prop="name" label="轮播图标题" align="center" />
                <el-table-column label="轮播图" align="center">
                    <template #default="scope">
                        <img v-if="scope.row.url !== null && scope.row.url !== ''" :src="scope.row.url"
                            style="width: 100px;height: 100px;">
                    </template>
                </el-table-column>

                <el-table-column prop="sort" label="排序" align="center" />
                <el-table-column prop="status" label="状态" align="center" />
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button plain type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button plain type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
        </el-card>

        <!-- 修改弹框 -->
        <el-dialog :title="dialog.title" v-model="dialog.visible" width="30%">
            <el-form :model="editForm" label-width="160px">
                <el-form-item label="轮播图标题">
                    <el-input v-model="editForm.name" />
                </el-form-item>

                <el-form-item label="图片">
                    <single-uploadp v-model="<string>editForm.url" />
                    <div></div>
                </el-form-item>


                <el-form-item label="排序">
                    <el-input-number v-model="<number>editForm.sort" :min="1" :max="999" />
                </el-form-item>


            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editDialogVisible = false">
                        取 消
                    </el-button>
                    <el-button type="primary" @click="editItem">
                        确 定
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import BannersAPI from "@/api/banner";

import {

    BannerForm,
    Banner,
    BannerPageResult,
    BannerQuery,
    BannerPageVO
} from "@/api/banner/model";
import SingleUpload from "@/components/Upload/SingleUploadp.vue";

defineOptions({
    name: "Banner",
    inheritAttrs: false,
});





const queryFormRef = ref(ElForm);


const editForm = reactive<BannerForm>({

});

const loading = ref(false);
const editDialogVisible = ref(false);


const total = ref(0);
// 搜索参数
const queryParams = reactive<BannerQuery>({
    pageNum: 1,
    pageSize: 10,
    name: '',
});

const BannerPageData = ref<BannerPageVO[]>();

const dialog = reactive({
    title: "",
    visible: false,
});

/**
 * 打开新增弹框
 */
function add() {
    editForm.id = null
    editForm.createUser = null;
    editForm.createTime = null;
    editForm.updateTime = null;
    editForm.sort = null;
    dialog.visible = true;
}
/**
 * 打开修改弹框
 * @param row 
 */
function handleEdit(row: Banner) {
    editForm.id = row.id;
    editForm.createUser = row.createUser;
    editForm.createTime = row.createTime;
    editForm.updateTime = row.updateTime
    editForm.sort = row.sort
    dialog.visible = true;
}

/** 查询 */
function handleQuery() {
    loading.value = true;
    BannersAPI.getPage(queryParams)
        .then((data) => {
            BannerPageData.value = data.records;
            total.value = data.total;
        })
        .finally(() => {
            loading.value = false;
        });
}
/**
 * 保存修改
 */
function editItem() {
    // 当id存在时执行修改操作，否则执行新增操作
    if (editForm.id) {
        BannersAPI.update(editForm).then(() => {
            handleQuery();
            dialog.visible = false;
        })
    } else {
        BannersAPI.add(editForm).then(() => {
            handleQuery();
            dialog.visible = false;
        })
    }

}

function handleDelete(row: Banner) {
    // 执行删除操作
    BannersAPI.delete(row).then(() => {
        handleQuery();
    })

}









onMounted(() => {
    handleQuery();
});
</script>